package online.yysh.charging;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.bean.request.PortIsAvailableRequest;
import online.yysh.charging.bean.response.GetPortAvailableResponse;
import online.yysh.charging.utils.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class InputDeviceNumberActivity extends BaseActivity {
    private TextView tv_inputDeviceNumber;
    private EditText et_number;
    private ImageView iv_scanCharging;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_input_device_number;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        et_number = findViewById(R.id.et_number);
        tv_inputDeviceNumber = findViewById(R.id.tv_title);
        tv_inputDeviceNumber.setText(R.string.input_device_number_charging);
        iv_scanCharging = findViewById(R.id.iv_scanCharging);
        iv_scanCharging.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(InputDeviceNumberActivity.this, ScanAndChargeActivity.class));
                finish();
            }
        });
    }

    @Override
    protected void initData() {

    }

    public void scanCharging(View view) {
        String number = et_number.getText().toString().trim();
        if (TextUtils.isEmpty(number)) {
            showToast("终端编号为空");
            return;
        }
        if (number.startsWith("A") || number.startsWith("B")) {
            String port = number.substring(0, 1);
            String deviceId = number.substring(1);
            int portInt = 66;
            if ("A".equals(port)) {
                portInt = 65;
            }
            isAvailable(deviceId, portInt);
        } else {
            showToast("终端编号输入不正确!");
        }
    }

    public void isAvailable(final String deviceId, final int port) {
        PortIsAvailableRequest request = new PortIsAvailableRequest(openid, deviceId, port);
        HttpUtil.getInstance().isAvailable(request, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                logE("result:" + result);
                GetPortAvailableResponse getPortAvailableResponse = new Gson().fromJson(result, GetPortAvailableResponse.class);
                if ("ok".equals(getPortAvailableResponse.getStatus())) { //可用
                    Intent intent = new Intent(InputDeviceNumberActivity.this, SelectChargingModeActivity.class);
                    intent.putExtra("chargingData", deviceId + "_" + port);
                    startActivity(intent);
                    finish();
                } else {
                    //桩不可用
                    final String data = getPortAvailableResponse.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast(data);
                        }
                    });
                    finish();
                }
            }
        });
    }

//    Intent intent = new Intent(ScanAndChargeActivity.this, SelectChargingModeActivity.class);
//            intent.putExtra("chargingData", deviceId + "_" + port);
//    startActivity(intent);
//    finish();
}